package com.dragon.mmochat.model.battle;

import java.io.Serializable;

import com.dragon.mmochat.model.enumType.MmochatActionType;
import com.dragon.mmochat.model.enumType.MmochatObjectType;
import com.dragon.mmochat.model.enumType.MmochatPropType;
import com.dragon.mmochat.model.enumType.MmochatSmeType;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.role.MmochatRole;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.util.MmochatUtil;

//战斗中某个玩家的一次操作
public class MmochatAction implements Serializable, Comparable<MmochatAction> {
	private static final long serialVersionUID = 2279959080252671600L;
	private int battleId = 0;
	private short turn = 1;
	private byte pos;
	private MmochatActionType actionType;
	private int addBlood = 0; // NPC补血用
	private byte targetPos;
	private int gridId = -1;
	private MmochatRole role;

	public int getBattleId() {
		return battleId;
	}

	public void setBattleId(int battleId) {
		this.battleId = battleId;
	}

	public short getTurn() {
		return turn;
	}

	public void setTurn(short turn) {
		this.turn = turn;
	}

	public byte getPos() {
		return pos;
	}

	public void setPos(byte pos) {
		this.pos = pos;
	}

	public MmochatActionType getActionType() {
		return actionType;
	}

	public void setActionType(MmochatActionType actionType) {
		this.actionType = actionType;
	}

	public byte getTargetPos() {
		return targetPos;
	}

	public void setTargetPos(byte targetPos) {
		this.targetPos = targetPos;
	}

	public int getGridId() {
		return gridId;
	}

	public void setGridId(int gridId) {
		this.gridId = gridId;
	}

	public MmochatRole getRole() {
		return role;
	}

	public void setRole(MmochatRole role) {
		this.role = role;
	}

	public int compareTo(MmochatAction other) {
		// 未被障碍的丹药使用者优于普通操作
		int addition = 10000000;
		int randSpeed = MmochatUtil.getRandomValue(61);
		int mySpeed = role.getSpeed() - 30 + randSpeed;

		if (getActionType() == MmochatActionType.道具) {
			MmochatRole myRole = role;
			if (myRole != null && !myRole.hasSme(MmochatSmeType.封印)
					&& !myRole.hasSme(MmochatSmeType.冰冻)
					&& !myRole.hasSme(MmochatSmeType.混乱)
					&& !myRole.hasSme(MmochatSmeType.死亡)) {
				MmochatPlayer owner = null;
				if (myRole instanceof MmochatPlayer) {
					owner = (MmochatPlayer) myRole;
				} else if (myRole instanceof MmochatPet) {
					MmochatPet pet = (MmochatPet) myRole;
					MmochatBattle battle = MmochatBattleService.battles
							.get(battleId);
					if (battle != null) {
						for (MmochatRole role : battle.getRoles().values()) {
							if (role instanceof MmochatPlayer) {
								MmochatPet myPet = ((MmochatPlayer) role)
										.getPetInBattle();
								if (myPet == pet) {
									owner = (MmochatPlayer) role;
									break;
								}
							}
						}
					}
				}
				if (owner != null) {
					MmochatPackageGrid grid = owner.getMyPackage().get(
							getGridId());
					if (grid != null && grid.getObject() != null) {
						MmochatCommonObject prop = grid.getObject();
						if (prop.getPropType() != null
								&& (prop.getPropType() == MmochatPropType.三魂丹
										|| prop.getPropType() == MmochatPropType.七魄丹
										|| prop.getPropType() == MmochatPropType.妙手回春丹 || prop
										.getPropType() == MmochatPropType.魂魄归位丹)) {
							mySpeed += addition;
						}
					}
				}

			}
		}

		randSpeed = MmochatUtil.getRandomValue(61);
		int hisSpeed = other.getRole().getSpeed() - 30 + randSpeed;
		if (other.getActionType() == MmochatActionType.道具) {
			MmochatRole myRole = other.getRole();
			if (myRole != null && !myRole.hasSme(MmochatSmeType.封印)
					&& !myRole.hasSme(MmochatSmeType.冰冻)
					&& !myRole.hasSme(MmochatSmeType.混乱)
					&& !myRole.hasSme(MmochatSmeType.死亡)) {
				MmochatPlayer owner = null;
				if (myRole instanceof MmochatPlayer) {
					owner = (MmochatPlayer) myRole;
				} else if (myRole instanceof MmochatPet) {
					MmochatPet pet = (MmochatPet) myRole;
					MmochatBattle battle = MmochatBattleService.battles
							.get(battleId);
					if (battle != null) {
						for (MmochatRole role : battle.getRoles().values()) {
							if (role instanceof MmochatPlayer) {
								MmochatPet myPet = ((MmochatPlayer) role)
										.getPetInBattle();
								if (myPet == pet) {
									owner = (MmochatPlayer) role;
									break;
								}
							}
						}
					}
				}
				if (owner != null) {
					MmochatPackageGrid grid = owner.getMyPackage().get(
							other.getGridId());
					if (grid != null && grid.getObject() != null) {
						MmochatCommonObject prop = grid.getObject();
						if (prop.getPropType() != null
								&& (prop.getPropType() == MmochatPropType.三魂丹
										|| prop.getPropType() == MmochatPropType.七魄丹
										|| prop.getPropType() == MmochatPropType.妙手回春丹 || prop
										.getPropType() == MmochatPropType.魂魄归位丹)) {
							hisSpeed += addition;
						}
					}
				}
			}
		}

		return hisSpeed - mySpeed;
	}

	public int getAddBlood() {
		return addBlood;
	}

	public void setAddBlood(int addBlood) {
		this.addBlood = addBlood;
	}

}
